var subId = '';    /*当前领域所属id*/
/*判断用户是否有未做完题目*/
$.ajax({
	type:'get',
    url:url() + '/quiz/quizIsContinuing',
    data:{
    },
    dataType:'json',
    success:function(ret,sta,xhr){
		var sessionstatus = xhr.getResponseHeader('redirectStatus');
		if(sessionstatus == '302'){
			window.location.href = url() + '/wxuser/goCompleteInfo';
			return false;
		};
    	var ret_obj = JSON.parse(ret.result)
    	console.log(ret)
    	console.log(ret_obj)
    	if(ret.code == 0 && ret_obj.status == 0){
    		if(ret_obj.object == 1){
    			$.ajax({
    				type:'post',
    				url:url() + '/quiz/fetch2',
    				data:{
				    	catId:0,
				    	quizUuid:'',
				    	betMoney:0
    				},
    				dataType:'json',
    				success:function(reu,sta,xhr){
						var sessionstatus = xhr.getResponseHeader('redirectStatus');
						
						if(sessionstatus == '302'){
							window.location.href = url() + '/wxuser/goCompleteInfo';
							return false;
						};
    					var reu_obj = JSON.parse(reu.result);
    					console.log(reu_obj)
    					if(reu.code == 0 && reu_obj.status == 0){
    						/*题目存储subject*/
							localStorage.subject = reu.result;
							/*当前题题目*/
							localStorage.now_title = reu_obj.object.title;
							window.location.href = url() + '/htm/subject.html'
    					}else{
    						alert(reu_obj.message)
    					}
    				},
    				error:function(){
    					console.log('error');
    				}
    			})
    		}
    	}
    },
    error:function(){
		console.log('error')
    }
});

$(function(){
	/*页面初始化加载数据*/
	$.ajax({
		type:'get',
		url:url() + "/quiz/begin2",
		data:{},
		dataType:'json',
		success:function(res,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			//console.log(result);
			result = JSON.parse(res.result);
			console.log(result);
			if(res.code == 0 && result.status != 0){
				alert(result.message)
			};
			/*余额*/
			balance = (result.object.userBalance.mainMoney + result.object.userBalance.bonusMoney + result.object.userBalance.platformBonus) - (result.object.userBalance.frozenMainMoney+result.object.userBalance.frozenBonusMoney+result.object.userBalance.frozenPlatformBonus);
			$('.bal').html(balance.toFixed(2)+' <em class="fs-24">元</em>');
			localStorage.user_balance = balance;
			/*免费题目*/
			if(result.object.freeQuizList.length>0){
				index(result.object.freeQuizList[0].categories);
				musval('free',result.object);
				$('.free button').click(function(){
					var free_obj = {
						id:$('.free-sub').attr('data-id'),
						uuid:$('.free-sub').attr('uuid'),
						money:0
					};
					startGame(free_obj);
				});
			}else{
				$('.free').hide();
			};
			
			/*领域列表*/
			var field_now = '';    /*当前领域*/
			for(var i=0;i<result.object.categoryList.length;i++){
				if(result.object.categoryList[i].focusedFlg == 1){
					$('.fel-now').text('当前领域：'+result.object.categoryList[i].name);
					field_now = result.object.categoryList[i].name;
				};
			};
			musval('field',result.object,1);
			/*对领域项附加默认样式*/
			$('.fel-cho span').each(function(){
				if($(this).text() == field_now){
					$(this).addClass('active');
				}
			});
			/*领域列表初始化事件*/
			intall();
			
			/*当前领域下题目*/
			if(result.object.quizList.length>0){
				subId = result.object.quizList[0].categories[0].id;
				musval('sub-topic',result.object,0);

				/*领域列表点击弹窗*/
				$('.subject .row').each(function(key){
					$(this).on('click',function(){
						var obj = {
							Title:result.object.quizList[key].title,
							userName:result.object.quizList[key].authorName,
							usertitle:result.object.quizList[key].authorTitle,
							level:result.object.quizList[key].difficultLevel,
							time:result.object.quizList[key].allowedTime,
							couList:result.object.quizList[key].couponList,
							money:result.object.quizList[key].betMoney,
							categ:result.object.quizList[key].categories,
							uuid:result.object.quizList[key].uuid,
							id:result.object.quizList[key].categories[0].id
						};
						musval('start-cont',obj,0);
						$('.start').show();
						/*初始化弹层事件*/
						coupon(obj.couList);
					})
				});
				
			}else{
				$('.subject .sub-topic').hide();
				$('.subject .sub-nothing').show();
			};
			/*换一换*/
			$('.sub-cag').click(function(){
				$.ajax({
					type:'post',
					url:url() + "/quiz/refresh",
					data:{
						catId:subId
					},
					dataType:'json',
					success:function(result,sta,xhr){
						var sessionstatus = xhr.getResponseHeader('redirectStatus');
						
						if(sessionstatus == '302'){
							window.location.href = url() + '/wxuser/goCompleteInfo';
							return false;
						};
						var val = JSON.parse(result.result);
						if(result.code != 0 && val.status != 0){
							alert(val.message)
						};
						//console.log(val)
						musval('sub-topic',val.object,0);
						/*数字等级转换文本*/
						$('.difficultLevel').each(function(){
							$(this).text(difficultLevel($(this).text()))
						});
						/*领域列表点击弹窗*/
						$('.subject .row').each(function(key){
							$(this).on('click',function(){
								var obj = {
									Title:val.object.quizList[key].title,
									userName:val.object.quizList[key].authorName,
									usertitle:val.object.quizList[key].authorTitle,
									level:val.object.quizList[key].difficultLevel,
									time:val.object.quizList[key].allowedTime,
									couList:val.object.quizList[key].couponList,
									money:val.object.quizList[key].betMoney,
									categ:val.object.quizList[key].categories,
									uuid:val.object.quizList[key].uuid,
									id:val.object.quizList[key].categories[0].id
								};
								musval('start-cont',obj,0);
								$('.start').show();
								/*初始化弹层事件*/
								coupon(obj.couList);
									
							})
						});
					},
					error:function(){
						console.log('error')
					}
				})
			});
			/*其他领域*/
			if(result.object.recommendedQuizList.length>0){
				for(var i=0;i<result.object.recommendedQuizList.length;i++){
					index(result.object.recommendedQuizList[i].categories);
				};
				musval('oth-fie',result.object,0);
				/*其他领域列表点击弹窗*/
				$('.oth-fie > .col-xs-6').each(function(key){
					$(this).click(function(){
						var obj = {
							Title:result.object.recommendedQuizList[key].title,
							userName:result.object.recommendedQuizList[key].authorName,
							usertitle:result.object.recommendedQuizList[key].authorTitle,
							level:result.object.recommendedQuizList[key].difficultLevel,
							time:result.object.recommendedQuizList[key].allowedTime,
							couList:result.object.recommendedQuizList[key].couponList,
							money:result.object.recommendedQuizList[key].betMoney,
							categ:result.object.recommendedQuizList[key].categories,
							uuid:result.object.recommendedQuizList[key].uuid,
							id:result.object.recommendedQuizList[key].categories[0].id
						};
						musval('start-cont',obj,0);
						$('.start').show();
						/*初始化弹层事件*/
						coupon(obj.couList);
					})
				});
			}else{
				$('.other').hide();
			}
			
			/*数字等级转换文本*/
			$('.difficultLevel').each(function(){
				$(this).text(difficultLevel($(this).text()))
			});
				
		},
		error:function(){
			console.log('error')
		}
	});
	/*弹层禁止滑动同时不影响优惠券的滑动*/
	$('.sta-bg')[0].addEventListener("touchmove",function(e){
		e.preventDefault();
	},true);
	/*关闭弹层*/
	$('.sta-bg').click(function(){
		$('.start').hide();
	});
	/*余额不足*/
	$('.balance-lack .yes').click(function(){
		window.location.href = url() + '/htm/recharge.html'
	});
	$('.balance-lack .no').click(function(){
		$('.balance-lack').hide();
	});
	$('.balance-lack')[0].addEventListener("touchmove",function(e){
		e.preventDefault();
	},true);
});
/*初始化事件*/
function intall(){
		/*领域选择展示合并*/
		$('.fel-btn').click(function(){
			if($(this)[0].className.indexOf('top') != -1){
				$(this).removeClass('top');
				$('.fel-btn').css('top','');
				$('.fel-cho').hide();
			}else{
				$(this).addClass('top');
				$('.fel-cho').css('top',$('.fel-now').height()+'px');
				$('.fel-cho').show();
				$('.fel-btn').css('top',$('.fel-now').height()+$('.fel-cho').height()+'px');
			}
		});
		/*领域选择赋值*/
		$('.fel-cho span').each(function(){
			$(this).click(function(){
				$(this).addClass('active');
				$(this).siblings().removeClass('active');
				$('.fel-btn').removeClass('top');
				$('.fel-btn').css('top','');
				$('.fel-now').text('当前领域：'+$(this).text());
				$('.fel-cho').hide();
				var id = $(this).attr('data-id');
				$.ajax({
					type:'post',
					url:url() + "/quiz/begin2",
					data:{
						catId:id
					},
					dataType:'json',
					success:function(result,sta,xhr){
						var sessionstatus = xhr.getResponseHeader('redirectStatus');
						
						if(sessionstatus == '302'){
							window.location.href = url() + '/wxuser/goCompleteInfo';
							return false;
						};
						subId = id;
						var rel = JSON.parse(result.result);
						console.log(rel);
						if(result.code == 0 && rel.status == 0){
							/*当前领域下题目*/
							if(rel.object.quizList.length>0){
								$('.subject .sub-topic').show();
								$('.subject .sub-nothing').hide();
								musval('sub-topic',rel.object,0);

								/*领域列表点击弹窗*/
								$('.subject .row').each(function(key){
									$(this).on('click',function(){
										var obj = {
											Title:rel.object.quizList[key].title,
											userName:rel.object.quizList[key].authorName,
											usertitle:rel.object.quizList[key].authorTitle,
											level:rel.object.quizList[key].difficultLevel,
											time:rel.object.quizList[key].allowedTime,
											couList:rel.object.quizList[key].couponList,
											money:rel.object.quizList[key].betMoney,
											categ:rel.object.quizList[key].categories,
											uuid:rel.object.quizList[key].uuid,
											id:rel.object.quizList[key].categories[0].id
										};
										musval('start-cont',obj,0);
										$('.start').show();
										/*初始化弹层事件*/
										coupon(obj.couList);
									})
								});
								
							}else{
								$('.subject .sub-topic').hide();
								$('.subject .sub-nothing').show();
							};
							/*其他领域*/
							if(rel.object.recommendedQuizList.length>0){
								for(var i=0;i<rel.object.recommendedQuizList.length;i++){
									index(rel.object.recommendedQuizList[i].categories);
								};
								$('.other').show();
								musval('oth-fie',rel.object,0);
								/*其他领域列表点击弹窗*/
								$('.oth-fie > .col-xs-6').each(function(key){
									$(this).click(function(){
										var obj = {
											Title:rel.object.recommendedQuizList[key].title,
											userName:rel.object.recommendedQuizList[key].authorName,
											usertitle:rel.object.recommendedQuizList[key].authorTitle,
											level:rel.object.recommendedQuizList[key].difficultLevel,
											time:rel.object.recommendedQuizList[key].allowedTime,
											couList:rel.object.recommendedQuizList[key].couponList,
											money:rel.object.recommendedQuizList[key].betMoney,
											categ:rel.object.recommendedQuizList[key].categories,
											uuid:rel.object.recommendedQuizList[key].uuid,
											id:rel.object.recommendedQuizList[key].categories[0].id
										};
										musval('start-cont',obj,0);
										$('.start').show();
										/*初始化弹层事件*/
										coupon(obj.couList);
									})
								});
							}else{
								$('.other').hide();
							};
							/*数字等级转换文本*/
							$('.difficultLevel').each(function(){
								$(this).text(difficultLevel($(this).text()))
							});
						}else{
							alert(rel.message)
						}
							
					},
					error:function(){
						console.log('error')
					}
				})
			})
		});
		
	}
/*弹层事件*/
function coupon(){
	$('.start .difficultLevel').each(function(){
		$(this).text(difficultLevel($(this).text()))
	});
	$('.start button').click(function(){
		var star_obj = {
			id:$(".sta-cont").attr('data-id'),
			uuid:$(".sta-cont").attr('uuid'),
			money:$(".sta-cont").attr('bel')
		};
		startGame(star_obj)
	})
}
/*开始游戏*/
function startGame(obj){
	var coup = 0;
	$('.sta-voc span').each(function(){
		if($(this).className != 'no'){
			coup = $(this).attr('data-id')
		}
	})
	$.ajax({
		type:'post',
		url:url() + "/quiz/fetch2",
		data:{
			catId:obj.id,
			quizUuid:obj.uuid,
			betMoney:obj.money,
			couponId:coup
		},
		dataType:'json',
		success:function(result,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			console.log(result);
			var res = JSON.parse(result.result)
			console.log(res);
			//console.log(obj.id+'---'+obj.uuid+'---'+obj.money);
			if(result.code == 0 && res.status == 0){
				/*题目存储subject*/
				localStorage.subject = result.result;
				/*当前题题目*/
				localStorage.now_title = res.object.title;
				/*分享地址参数id*/
				localStorage.quizInstanceId = res.object.quizInstanceId;
				localStorage.user_balance = localStorage.user_balance - obj.money;
				localStorage.rec_money = obj.money;
				
				window.location.href = url() + '/htm/subject.html';
			}else if(result.code == 0 && res.status == -12580){
				/*题目存储subject参数对象*/
				obj.coup = coup;
				localStorage.sub_canshu = JSON.stringify(obj);
				$('.start').hide();
				$('.balance-lack').show();
				localStorage.rec_money = obj.money;
				
			}else{
				alert(res.message);
			}
		},
		complete:function(xhr,ts){
			//console.log(xhr)
			//获取用户被禁时返回的header
			
		},
		error:function(){
			
		}
	})
}